/*
 * MyEncriptorGUI.java
 *
 * Created on 2010年12月26日, 下午1:10
 */

package myEncriptor;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  heantai
 */
public class MyEncriptorGUI extends javax.swing.JFrame {
    private File keyFile,tobeEncripted,saved;
    private static MyEncriptor myEncriptor = MyEncriptor.instance();
    /** Creates new form MyEncriptorGUI */
    public MyEncriptorGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jButton4 = new javax.swing.JButton();
    jTextField4 = new javax.swing.JTextField();
    jProgressBar1 = new javax.swing.JProgressBar();
    jTextField5 = new javax.swing.JTextField();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem2 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    setResizable(false);

    jButton1.setText("密钥文件");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    jButton2.setText("加密／解密");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });

    jButton3.setText("保存为");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton3ActionPerformed(evt);
      }
    });

    jTextField1.setEditable(false);

    jTextField2.setEditable(false);

    jTextField3.setEditable(false);

    jButton4.setText("开始加密／解密");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton4ActionPerformed(evt);
      }
    });

    jTextField4.setEditable(false);
    jTextField4.setFont(new java.awt.Font("Dialog", 0, 18));
    jTextField4.setText("欢迎使用文件加密器");
    jTextField4.setAutoscrolls(false);
    jTextField4.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    jTextField4.setCaretColor(new java.awt.Color(255, 255, 255));
    jTextField4.setDisabledTextColor(new java.awt.Color(0, 0, 0));
    jTextField4.setEnabled(false);

    jProgressBar1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    jProgressBar1.setBorderPainted(false);

    jTextField5.setEditable(false);
    jTextField5.setAutoscrolls(false);
    jTextField5.setBorder(null);

    jMenu1.setText("File");

    jMenuItem1.setText("离开");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem1ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem1);

    jMenuBar1.add(jMenu1);

    jMenu2.setText("Help");

    jMenuItem2.setText("说明");
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem2ActionPerformed(evt);
      }
    });
    jMenu2.add(jMenuItem2);

    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGap(34, 34, 34)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jButton4)
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGap(99, 99, 99)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(jProgressBar1, 0, 0, Short.MAX_VALUE))
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))))
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
            .addComponent(jTextField3)
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addGap(26, 26, 26))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGap(24, 24, 24)
        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(27, 27, 27)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .addGap(35, 35, 35))
          .addGroup(layout.createSequentialGroup()
            .addComponent(jButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
          .addComponent(jButton2))
        .addGap(31, 31, 31)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .addGap(47, 47, 47))
          .addComponent(jButton3))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jButton4)
          .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap(273, Short.MAX_VALUE)
        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(32, 32, 32))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    JFileChooser chooser= new JFileChooser();
    int option=chooser.showOpenDialog(null);
    if(option==JFileChooser.APPROVE_OPTION){
        keyFile = chooser.getSelectedFile();
    }
    if(keyFile!=null){
        jTextField1.setText(keyFile.getAbsolutePath());
    }
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    JFileChooser chooser= new JFileChooser();
    int option=chooser.showOpenDialog(null);
    if(option==JFileChooser.APPROVE_OPTION){
        tobeEncripted = chooser.getSelectedFile();
    }
    if(tobeEncripted!=null){
        jTextField2.setText(tobeEncripted.getAbsolutePath());
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    JFileChooser chooser= new JFileChooser();
    int option=chooser.showOpenDialog(null);
    if(option==JFileChooser.APPROVE_OPTION){
        saved = chooser.getSelectedFile();
    }
    if(saved!=null){
        jTextField3.setText(saved.getAbsolutePath());
    }
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    if(myEncriptor.encripting){
        JOptionPane.showMessageDialog(null, "任务正在进行，请稍等");
        return;
    }
    if(keyFile==null){
        JOptionPane.showMessageDialog(null, "请重新选择密钥文件");
        return;
    }else if(!keyFile.exists()){
        JOptionPane.showMessageDialog(null, "密钥文件不存在");
        return;
    }
    if(tobeEncripted==null){
        JOptionPane.showMessageDialog(null, "请选择要加密或解密的文件");
        return;
    }else if(!tobeEncripted.exists()){
        JOptionPane.showMessageDialog(null, "请选择要加密或解密的文件");
        return;
    }
    if(saved==null){
        JOptionPane.showMessageDialog(null, "未选择要保存的文件");
        return;
    }
    
    jProgressBar1.setAutoscrolls(true);
    jProgressBar1.setIndeterminate(true);
    jProgressBar1.setBorderPainted(true);
    jProgressBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    jTextField5.setVisible(true);
    jTextField5.setText("加密／解密中");
    Thread startEncript = new Thread(){
            @Override
            public void run(){
                try{
                    myEncriptor.encripting = true;
                    myEncriptor.encript(keyFile, tobeEncripted, saved);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        };
        startEncript.setPriority(8);
        startEncript.start();
        
        Thread hideProgressBar = new Thread(){
            @Override
            public void run(){
                while(!myEncriptor.encripted){
                    yield();
                }
                myEncriptor.encripted=false;
                jProgressBar1.setAutoscrolls(false);
    jProgressBar1.setIndeterminate(false);
        jProgressBar1.setBorder(null);
        jProgressBar1.setBorderPainted(false);
        jTextField5.setText(null);
            }
        };
        hideProgressBar.setPriority(3);
        hideProgressBar.start();
    
}//GEN-LAST:event_jButton4ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    System.exit(0);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
// TODO add your handling code here:
    JOptionPane.showMessageDialog(null, "使用说明\n加密／解密按键\n对己加密的文件进行解密\n对未加密的文件进行加密");
}//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyEncriptorGUI().setVisible(true);
            }
        });
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JMenu jMenu1;
  private javax.swing.JMenu jMenu2;
  private javax.swing.JMenuBar jMenuBar1;
  private javax.swing.JMenuItem jMenuItem1;
  private javax.swing.JMenuItem jMenuItem2;
  private javax.swing.JProgressBar jProgressBar1;
  private javax.swing.JTextField jTextField1;
  private javax.swing.JTextField jTextField2;
  private javax.swing.JTextField jTextField3;
  private javax.swing.JTextField jTextField4;
  private javax.swing.JTextField jTextField5;
  // End of variables declaration//GEN-END:variables

}
